﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.DBAccess
{
	class JustNewDAO : BaseDAO
	{
		#region Basic Method

		public static DataTable GetAll()
		{
			return ExcuteProcedure(ToolConstants.StoreProcedure.JustNew.GetAll);
		}

		public static DataTable GetAll(int range)
		{
            //return ExcuteProcedure(ToolConstants.StoreProcedure.JustNew.GetAllWithDateRange);
            try
            {
                var parameters = new SqlParameter[]
                    {
						CreateParameter("@Range", SqlDbType.Int, range)
                    };
                return ExcuteProcedure(ToolConstants.StoreProcedure.JustNew.GetAllWithDateRange, parameters);

            }
            catch (Exception e)
            {
                throw new BaseDAO.MyException(e);
            }
		}

		public static DataTable GetOne(int justNewId)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@ID", SqlDbType.Int, justNewId)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.JustNew.GetOne, parameters);

			}
			catch(Exception e)
			{
				throw new BaseDAO.MyException(e);
			}

		}

		public static JustNew LoadOne(int justNewId)
		{
			JustNew o = new JustNew();
			DataTable dt = GetOne(justNewId);
			if(dt.Rows.Count > 0)
			{
				o.ID = int.Parse(dt.Rows[0][ToolConstants.Entities.JustNew.FieldName.ID].ToString());
				o.Date = DateTime.Parse(dt.Rows[0][ToolConstants.Entities.JustNew.FieldName.Date].ToString());
				o.ToolID = dt.Rows[0][ToolConstants.Entities.JustNew.FieldName.ToolID].ToString();
				o.Quantity = !Utilities.IsNullOrEmpty(dt.Rows[0][ToolConstants.Entities.JustNew.FieldName.Quantity].ToString()) ? int.Parse(dt.Rows[0][ToolConstants.Entities.JustNew.FieldName.Quantity].ToString()) : 0;
				o.Note = dt.Rows[0][ToolConstants.Entities.JustNew.FieldName.Note].ToString();

			}

			return o;
		}


		public static int Insert(JustNew o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@Date", SqlDbType.DateTime, o.Date),
                        CreateParameter("@ToolID", SqlDbType.NVarChar, o.ToolID),
                        CreateParameter("@Quantity", SqlDbType.Int, o.Quantity),
                        CreateParameter("@Note", SqlDbType.NVarChar, o.Note),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.JustNew.Insert, parameters);

			}
			catch(Exception e)
			{
				throw new BaseDAO.MyException(e);
			}

		}

		public static int Update(JustNew o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@ID", SqlDbType.Int, o.ID),
                        CreateParameter("@Date", SqlDbType.DateTime, o.Date),
                        CreateParameter("@ToolID", SqlDbType.NVarChar, o.ToolID),
                        CreateParameter("@Quantity", SqlDbType.Int, o.Quantity),
                        CreateParameter("@Note", SqlDbType.NVarChar, o.Note),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.JustNew.Update, parameters);
			}
			catch(Exception e)
			{
				throw new BaseDAO.MyException(e);
			}
		}

		public static int Delete(JustNew o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@ID", SqlDbType.Int, o.ID)
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.JustNew.Delete, parameters);
			}
			catch(Exception e)
			{
				throw new BaseDAO.MyException(e);
			}
		}

		public static DataTable GetTemplateTable()
		{
			DataTable table = new DataTable("333_JustNew");

			return table;
		}

		#endregion
	}
}
